package com.dashan.servlets;

import static com.dashan.Constants.SUCCESS;

import java.io.IOException;
import java.util.Date;

import javax.jdo.JDODataStoreException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dashan.TestConstants;
import com.dashan.backend.DashanException;
import com.dashan.backend.FriendManager;
import com.dashan.backend.datastore.DsPosition;
import com.dashan.backend.datastore.DsStatus;
import com.dashan.backend.datastore.DsUser;
import com.dashan.backend.datastore.UserDao;
import com.dashan.backend.datastore.DsUserProfile.Gender;

@SuppressWarnings("serial")
public class AddTestingDataServlet extends DashanServlet {

    @Override
    public String getUrlPattern() {
        return "/addtestingdata";
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        UserDao dao = new UserDao();
        try {
            int i = 0;
            long now = new Date().getTime();
            for (String[] s : getTestData()) {
                DsUser user;
                user = dao.createUser(s[0], s[1], "");
                FriendManager.initializeUserGroup(user);
                user.setPosition(new DsPosition(user.getUserName(), Double.parseDouble(s[4]),
                        Double.parseDouble(s[5]), 0.0, new Date(now + (i++) * 100000)));
                user.setStatus(new DsStatus(user.getUserName(), s[2], new Date(now + (i++) * 100000)));
                user.getProfile().setGender(Gender.get(s[3]));

            }
        } catch (JDODataStoreException e) {
            throw new ServletException(e.getMessage());
        } catch (DashanException e) {
            throw new ServletException(e.getMessage());
        } finally {
            dao.commit();
        }
        
        dao = new UserDao();
        try {
            for (String s : getFriendPairs()) {
                String[] friendPair = s.split(",");
                FriendManager.addFriend(dao.getUser(friendPair[0]), friendPair[1]);
                FriendManager.addFriend(dao.getUser(friendPair[1]), friendPair[0]);
            }
        } catch (JDODataStoreException e) {
            throw new ServletException(e.getMessage());
        } catch (DashanException e) {
            throw new ServletException(e.getMessage());
        } finally {
            dao.commit();
        }
        
        resp.getWriter().print(SUCCESS);
    }

    /** [0]userName, [1]nickName, [2]Status, [3]Gender, [4]Lng, [5]Lat. */
    private static String[][] getTestData() {
        // userName, nickName, Status, Gender, Position
        return new String[][] {
                new String[] { "tl", "童姥", "不是她！不是她！不是她！", "FEMALE", "112.553945", "37.8612" },
                new String[] { "caz", "阿朱", "离开了你，你会孤零零的，我也会孤零零的！", "FEMALE", "112.5070853",
                        "37.79084668" },
                new String[] { "xf", "萧峰", "哈哈哈！慕容复公子，姓庄的，丁老怪，你们三个一起上，我萧峰何惧？", "MALE",
                        "112.6473024", "37.77497037" },
                new String[] { "yls", "岳老三", "这是世上岂能有大我三辈的人？", "MALE", "112.5919124", "37.76268041" },
                new String[] { "wyy", "王语嫣", "段公子，你对我的大恩，今生无以相报，但求来世吧！", "FEMALE", "112.5840462",
                        "37.86645095" },
                new String[] { "xz", "虚竹", "小僧念的是饮水咒。佛说每一碗水中，有八万四千条小虫，出家人戒杀，因此要念了饮水咒，这才喝得。",
                        "MALE", "112.5215588", "37.86256864" },
                new String[] { "dy", "段誉", "你不用保护我的，我和他们无冤无愁，不会为难我的！", "MALE", "112.6165688",
                        "37.85196004" },
                new String[] { "mrf", "慕容复", "要为复兴大燕为第一要务，收揽人心，为我所用！岂能为了一个箫峰，得罪天下英雄？", "MALE",
                        "112.6320745", "37.92623631" },
                new String[] { "wfw", "王夫人", "我让你捉那条老狗，你怎么给我捉条小狗回来。", "FEMALE", "112.6107897",
                        "37.84208945" },
                new String[] { "wld", "乌老大", "杀！管他个少林武当。", "MALE", "112.5156719", "37.8484856" } };
    }
    
    private static String[] getFriendPairs() {
        return new String[] {
            "xf,caz",
            "wyy,dy",
            "dy,wfw",
            "dy,wld",
            "dy,yls",
            "dy,xf",
            "dy,xz",
            TestConstants.TEST_USERNAME + ",caz",
            TestConstants.TEST_USERNAME + ",wyy",
            TestConstants.TEST_USERNAME + ",dy",
            TestConstants.TEST_USERNAME + ",xf",
        };
    }
}
